package com.example.dell.fastshopping.loginRegist;


import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.fastshopping.MainActivity;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.loginRegist.moldle.LoginRegistResetMoldle;
import com.example.dell.fastshopping.network.MyCallBack;
import com.example.dell.fastshopping.utils.DESUtils;
import com.example.dell.fastshopping.utils.RegexUtils;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.example.dell.fastshopping.views.InputCode;
import com.example.dell.fastshopping.views.InputPassword;
import com.example.dell.fastshopping.views.MyEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by i7-1 on 2018/11/23.
 */

public class RegistActivity extends AppCompatActivity implements MyCallBack {

    @BindView(R.id.phone)
    MyEditText phone;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.input_password)
    InputPassword inputPassword;
    @BindView(R.id.perform_password)
    InputPassword performPassword;
    @BindView(R.id.code)
    InputCode code;
    @BindView(R.id.regist)
    TextView regist;
    @BindView(R.id.invite_code)
    MyEditText inviteCode;


    private LoginRegistResetMoldle moldle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        moldle = ViewModelProviders.of(this).get(LoginRegistResetMoldle.class);
        code.setCallBack(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.regist, R.id.back})
    public void onClick(View view) {
        if (view.getId() == R.id.back) {
            finish();
            return;
        }
        if (!RegexUtils.isEmpty(inputPassword.getText()) && inputPassword.getText().equals(performPassword.getText())) {
            final String phoneS = phone.getText().toString();
            final String pws = inputPassword.getText();
            moldle.regist(phoneS, pws, code.getText(), inviteCode.getText().toString()).observe(this, new Observer<Boolean>() {
                @Override
                public void onChanged(@Nullable Boolean aBoolean) {
                    if (aBoolean) {
                        toLogin(phoneS, pws);
                    }
                }
            });
        } else {
            ToastUtils.showShort("请检查密码是否正确");
        }

    }

    private void toLogin(String phoneS, String pws) {
        moldle.login(phoneS.trim(), "", pws).observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                if (aBoolean) {
                    Intent intent = new Intent(RegistActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    @Override
    public void callback() {
        moldle.getRegistCode(phone.getText().toString()).observe(RegistActivity.this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                if (aBoolean) {
                    code.start();
                }
            }
        });
    }
}
